//
//  LHTabBarController.swift
//  Swift3.0WB
//
//  Created by 美食家 on 17/3/28.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import UIKit

/** 主控制器  */
class LHTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        setupControllers()
    }

  }


// MARK: 设置界面

extension LHTabBarController{
    
    func setupControllers()-> Void{
        
    let array=[
        ["clsname": "LHHomeController",     "title": "首页", "imageName":"home"],
        ["clsname": "LHDiscoverController", "title": "发现", "imageName":"discover"],
        ["clsname": "LHMessageController",  "title": "消息", "imageName":"message_center"],
        ["clsname": "LHProfileControllerViewController", "title": "我","imageName":"profile"],
        ]
    
        var arrayM=[UIViewController]()
        
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
        
        
    }
    
    private func controller(dict:[String:String]) -> UIViewController{
        
//        let cName=Bundle.main.namespace+"."+dict["clsname"]! as String
//        print("----------------"+"\(NSClassFromString(() as? UIViewController.Type)")
//            let name=NSStringFromClass(cName) as? UIViewController.Type
        
        
        
        /** 1 取得字典内容 */
        guard let className=dict["clsname"],
               let title=dict["title"],
               let imageName=dict["imageName"]
        else {
            return UIViewController()
        }
        
        /** 2 设置值 */
        guard let vc=NSClassFromString((Bundle.main.namespace+"."+className) as String) as? UIViewController.Type else {
            print("失败")
            return UIViewController()
        }
        let v = vc.init()
        
        v.title=title;
        
        v.tabBarItem.image=UIImage(named:"tabbar_"+imageName)
        v.tabBarItem.selectedImage=UIImage(named:"tabbar_"+imageName+"_selected")?.withRenderingMode(.alwaysOriginal)
        
        v.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange],
                                            for: .highlighted)
        v.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)],
                                            for: UIControlState(rawValue:0))
        
        let nav = LHNavigationController(rootViewController: v)
        
        /** 3 返回控制器 */
        return nav
        
    }
}
